﻿namespace LeetCodeProject._0001_1000._001_100._61_70._067;

public class Solution
{
    public string AddBinary(string a, string b)
    {
        int len = a.Length - 1;
        int len1 = b.Length - 1;
        string result = "";
        int scale = 0;
        while (len >= 0 || len1 >= 0)
        {
            int num = 0;
            if (len >= 0)
            {
                num += int.Parse(a.Substring(len, 1));
            }

            if (len1 >= 0)
            {
                num += int.Parse(b.Substring(len1, 1));
            }

            num += scale;
            if (num < 2)
            {
                result = result.Insert(0, num.ToString());
                scale = 0;
            }
            else if (num < 3)
            {
                result = result.Insert(0, "0");
                scale = 1;
            }
            else
            {
                result = result.Insert(0, "1");
                scale = 1;
            }

            len--;
            len1--;
        }

        if (scale > 0)
        {
            result = result.Insert(0, "1");
        }

        return result;
    }
}